function rdata_out = rslice_determine_timestep_info ( rdata )
% RSLICE_DETERMINE_TIMESTEP_INFO:  retrieve all the timesteps and associated metadata
%
% PARAMETERS
% Input:
%     rdata:  metadata for rslice
% Output:
%     rdata_out:  rdata plus addition fields
%         ocean_time:  
%             All time values from the dataset.  Units are matlab native datenum. 
%         max_num_timesteps:           
%             length of ocean time.  I don't think this is really 
%             needed anymore.
%         ncfile_start_index, ncfile_stop_index:
%             This is needed in the case that the input is a directory
%             of files.  Each entry corresponds to one netcdf file and
%             gives its starting and stopping index in the overall 
%             time record.

rdata_out = rdata;

%
% How many timesteps?
switch rdata.input_type
case 'directory'
	ncfile =  rdata.ncfile_list{rdata.ncfile_index};

	max_num_timesteps = 0;
	n = length(rdata.ncfile_list);
	start_index = zeros(n,1);
	stop_index = zeros(n,1);
	last_index = -1;
	ocean_time = zeros(n,1);

	time_units = nc_attget ( rdata.ncfile_list{1}, rdata.record_variable, 'units' );
	for j = 1:length(rdata.ncfile_list)

		this_ocean_time = nc_varget ( rdata.ncfile_list{j}, rdata.record_variable );
		num_times = length(this_ocean_time);
		max_num_timesteps = max_num_timesteps + num_times;
		start_index(j) = last_index + 1;
		stop_index(j) = start_index(j) + num_times - 1;
		last_index = stop_index(j);

		ocean_time(start_index(j)+1:stop_index(j)+1) = this_ocean_time;

	end

case { 'file', 'url' }
	ncfile =  rdata.ncfile_list{rdata.ncfile_index};

    if nc_isvar ( rdata.ncfile_list{1}, rdata.record_variable )
	    ocean_time = nc_varget ( rdata.ncfile_list{1}, rdata.record_variable );
    	time_units = nc_attget ( rdata.ncfile_list{1}, rdata.record_variable, 'units' );
    else

        %
        % Ok, there is no time variable.  Great.
        time_info = nc_getdiminfo ( rdata.ncfile_list{1}, rdata.time_dimension );
        ocean_time = [0:time_info.Length-1]';
        time_units = '';

    end
	num_times = length(ocean_time);
	max_num_timesteps = num_times;
	start_index = 0;
	stop_index = num_times-1;


otherwise
	msg = sprintf ( '%s:  unknown rdata.location type ''%s''.\n', mfilename, rdata.location );
	rslice_error ( msg );
end

ocean_time = rslice_time2matd ( ocean_time, time_units );




rdata_out.max_num_timesteps = max_num_timesteps;
rdata_out.ncfile_start_index = start_index;
rdata_out.ncfile_stop_index = stop_index;
rdata_out.ocean_time = ocean_time;

return
